package com.vonchenchen.android_renderer;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

/**
 * Created by lidechen on 2021/1/24.
 */
public class PermissionActivity extends AppCompatActivity {

    private static final String TAG = "PermissionActivity";

    public static final int PERMISSION_REQ_ID_CAMERA = 3;
    public static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = 1;
    public static final int PERMISSION_REQ_ID_READ_EXTERNAL_STORAGE = 2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_permission);
        checkSelfPermissions();

        findViewById(R.id.btn_renderer).setOnClickListener((v)-> {
            startActivity(new Intent(PermissionActivity.this, MainActivity.class));
        });

        findViewById(R.id.btn_capture).setOnClickListener((v)->{
            startActivity(new Intent(PermissionActivity.this, CaptureActivity.class));
        });

        findViewById(R.id.btn_test).setOnClickListener((v)->{
            startActivity(new Intent(PermissionActivity.this, TestActivity.class));
        });

        findViewById(R.id.btn_remote_renderer).setOnClickListener((v)->{
            startActivity(new Intent(PermissionActivity.this, RemoteRendererActivity.class));
        });
    }

    private boolean checkSelfPermissions() {
        return checkSelfPermission(Manifest.permission.CAMERA, PERMISSION_REQ_ID_CAMERA) &&
         checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE) &&
                checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE, PERMISSION_REQ_ID_READ_EXTERNAL_STORAGE) ;

    }

    public boolean checkSelfPermission(String permission, int requestCode) {

        Log.d(TAG, "checkSelfPermission " + permission + " " + requestCode);
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[], @NonNull int[] grantResults) {

        switch (requestCode) {
            case PERMISSION_REQ_ID_CAMERA:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    checkSelfPermission(Manifest.permission.CAMERA, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
                }
                break;
            case PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_READ_EXTERNAL_STORAGE);
                }
                break;

            case PERMISSION_REQ_ID_READ_EXTERNAL_STORAGE:
//                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                    checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_CAMERA);
//                }
                break;
        }
    }
}
